package com.itheima.reggie.controlle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.BaseContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @Autowired
    UserService userService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    OrderDetailService orderDetailService;

    /**
     * 订单下单
     */
    @PostMapping("submit")
    public R<String> saveOrder(@RequestBody Orders orders){
        ordersService.saveOrder(orders);
        //fanhui
        return R.success("下单成功");
    }

    /**
     * 后台订单查询
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize,
                                String number,
                                String beginTime,
                                String endTime){

        Page<Orders> ordersPage1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class)
                .like(StringUtils.isNotBlank(number), Orders::getNumber, number)
                .between(StringUtils.isNotBlank(beginTime)&&StringUtils.isNotBlank(endTime),Orders::getOrderTime,beginTime,endTime);
        ordersService.page(ordersPage1,queryWrapper);

        List<Orders> records = ordersPage1.getRecords();
        records.stream().map(itm -> {

            itm.setUserName(itm.getUserId()+"");

            return itm;
        }).collect(Collectors.toList());
        return R.success(ordersPage1);
    }

    /**
     * 前端分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> pages(Integer page, Integer pageSize){
        Page<OrdersDto> ordersDtoPage = ordersService.pages(page,pageSize);
       return R.success(ordersDtoPage);
    }

    /**
     * 后台派送
     */
    @PutMapping
    private R order(@RequestBody Orders orders){
        ordersService.update().eq("id",orders.getId())
                .set("status",3).update();
        return R.success("修改成功");
    }
}
